<?php

namespace App\Http\Controllers;

use App\Models\PhpTest;
use Illuminate\Http\Request;

class CurdController extends Controller
{

    public function add(Request $request)
    {
        $validatedData = $request->validate([
            'name' => 'required|string|max:255',
            'age' => 'required|integer|min:0',
            'birth_date' => 'required|date',
        ]);

        PhpTest::create($validatedData);

        return response()->json(['message' => 'Created Successfully'], 201);
    }


    public function get()
    {
        $phpTests = PhpTest::all();

        return response()->json($phpTests);
    }


    public function update(Request $request, $id)
    {
        $phpTest = PhpTest::findOrFail($id);

        $validatedData = $request->validate([
            'name' => 'sometimes|required|string|max:255',
            'age' => 'sometimes|required|integer|min:0',
            'birth_date' => 'sometimes|required|date',
        ]);

        $phpTest->update($validatedData);

        return response()->json(['message' => 'Updated Successfully']);
    }


    public function delete($id)
    {
        $phpTest = PhpTest::findOrFail($id);
        $phpTest->delete();

        return response()->json(['message' => 'Deleted Successfully']);
    }
}
